package com.xiaobai.netease_news.activities;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.constants.Constant;
import com.xiaobai.netease_news.services.TutorialMusicService;
import com.xiaobai.netease_news.utils.ScreenUtil;

public class TutorialActivity extends FragmentActivity
        implements Runnable, View.OnClickListener,
        CompoundButton.OnCheckedChangeListener, ServiceConnection {

    private int currentImg = 0;

    private ImageView imageView1;
    private int[] images = new int[]{R.mipmap.biz_ad_new_version1_img1,
            R.mipmap.biz_ad_new_version1_img2, R.mipmap.biz_ad_new_version1_img3,
            R.mipmap.biz_ad_new_version1_img4, R.mipmap.biz_ad_new_version1_img5,
            R.mipmap.biz_ad_new_version1_img6, R.mipmap.biz_ad_new_version1_img7};

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int imgIndex = msg.arg1;
            imageView2.setImageResource(images[imgIndex]);
            imageView1.setImageResource(images[imgIndex + 1 == 7 ? 0 : imgIndex + 1]);
            imageView2.startAnimation(animation);
        }
    };
    private CheckBox music;
    private TutorialMusicService.Controller controller;
    private ImageView imageView2;
    private Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tutorial);

        PackageManager packageManager = getPackageManager();
        int versionCode = 5;
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        SharedPreferences sharedPreferences = getSharedPreferences(Constant.SP_APP_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(Constant.SP_IS_TUTORIAL_SHOW + versionCode, true);
        editor.apply();

        imageView1 = (ImageView) findViewById(R.id.tutorial_imgview1);
        imageView2 = (ImageView) findViewById(R.id.tutorial_imgview2);
        ImageView start = (ImageView) findViewById(R.id.tutorial_start);
        ImageView share = (ImageView) findViewById(R.id.tutorial_share);
        music = (CheckBox) findViewById(R.id.tutorial_music);

        music.setOnCheckedChangeListener(this);
        start.setOnClickListener(this);
        share.setOnClickListener(this);

        Intent intent = new Intent(this, TutorialMusicService.class);
        bindService(intent, this, BIND_AUTO_CREATE);

        animation = AnimationUtils.loadAnimation(this, R.anim.anim_tutorial);

        new Thread(this).start();
    }

    @Override
    protected void onStop() {
        super.onStop();

        unbindService(this);
    }

    @Override
    public void run() {
        while (true) {
            currentImg = (currentImg + 1) % images.length;
            if (currentImg > images.length)
                currentImg = 0;

            Message message = Message.obtain();

            message.arg1 = currentImg;
            handler.sendMessage(message);

            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tutorial_start:
                startActivity(new Intent(this, MainActivity.class));
                if (controller != null) {
                    controller.stop();
                    controller.release();
                }
                this.finish();
                break;
            case R.id.tutorial_share:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("分享");
                builder.show();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            music.setBackgroundResource(R.mipmap.biz_new_version_music_closed);
            if (controller != null) {
                controller.pause();
            }
        } else {
            music.setBackgroundResource(R.mipmap.biz_new_version_music);
            if (controller != null) {
                controller.start();
            }
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        controller = (TutorialMusicService.Controller) service;
        controller.start();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        controller = null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            startActivity(new Intent(this, MainActivity.class));
            this.finish();
        }

        return true;
    }
}
